package com.airena.controller;

import com.airena.result.Result;
import com.airena.utils.MailUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 邮件接口
 */
@RestController
@RequestMapping("/mail")
@Slf4j
@Tag(name = "邮件接口")
public class MailController {
    
    @Autowired
    private MailUtil mailUtil;
    
    /**
     * 发送邮箱验证码
     * @param mail 邮箱地址
     * @return 发送结果
     */
    @PostMapping("/send-code")
    @Operation(summary = "发送邮箱验证码")
    public Result<String> sendVerificationCode(@RequestParam("mail") String mail) {
        try {
            // 生成并存储验证码
            String code = mailUtil.generateAndStoreVerificationCode(mail);
            
            // 发送邮件验证码
            mailUtil.sendVerificationCode(mail, code);
            
            return Result.success("验证码已发送至您的邮箱");
        } catch (Exception e) {
            log.error("发送验证码失败: {}", e.getMessage(), e);
            return Result.error("发送验证码失败: " + e.getMessage());
        }
    }
}